package cn.com.demo.web.login;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.com.common.constant.Constants;
import cn.com.common.pagination.Page;
import cn.com.demo.constant.GlobalProperty;
import cn.com.demo.pojo.TsTourGuide;
import cn.com.demo.pojo.User;
import cn.com.demo.service.TsTourGuideService;

/**
 * spring mvc中将controller认为是MVC中的控制层； 如果不基于注解该类需要继承CommandController或者其他
 * 如果基于注解：在类名前加上@Controller 将类名前加上该注解，当spring启动或者web服务启动，spring会自动扫描所有包
 * 作用是告诉服务器这个类可以接收用户请求，并处理请求 Version: 1.0
 */
@Controller
public class TsTourGuideController {

	@Autowired
	@Qualifier("TsTourGuideService")
	private TsTourGuideService tsTourGuideService;

	private void setCommonData(Model model) {
		// 设置通用属性
	}
	/**
	 * 客户端验证和后台验证配置的请求
	 * TsTourGuide tsTourGuide,BindingResult bindingResult 这两个对象必须紧挨着，否则会出错。他们bindingresult是把验证的错误信息存进他得errors属性里了
	 * @param request
	 * @param model
	 * @return 
	 */
	@RequestMapping(value = "/login", method={ RequestMethod.POST})
	public String login(HttpServletRequest request,@Valid @ModelAttribute("user") User user,BindingResult bindingResult,Model model) {
		String dysfz = user.getDysfz();
		String dyzhenghao = user.getDyzhenghao();
		TsTourGuide  guide=tsTourGuideService.checkGuidePassword(dysfz, dyzhenghao);
		if(null != guide){
			request.getSession().removeAttribute(GlobalProperty.SESSION_USER);
			request.getSession().setAttribute(GlobalProperty.SESSION_USER, guide);
			request.getSession().setAttribute(GlobalProperty.SESSION_USER_KEY, guide.getId());
			return "/index";
		} else{
//			Map<String, Object> model = bindingResult.getModel();
//			model.put("messages", "身份证或导游证号错误！请注意大小写是否正确!");
			bindingResult.rejectValue("dysfz", null, "身份证或导游证号错误！请注意大小写是否正确!");
			if(bindingResult.hasErrors()) {
				model.addAttribute("user",user);//把user对象返回到页面， 这样不至于表单被清空了
				model.addAttribute("error", "身份证或导游证号错误！请注意大小写是否正确!");
				return "forward:/login.jsp";
			}
			return null;
		}
	}

	/**
	 * 这个时候/tstourguide就是用户请求的，返回字符串就决定要显示MVC的哪个视图层
	 * 
	 * @return
	 */
	@RequestMapping(value = "/tstourguide", method = { RequestMethod.GET })
	public String list(HttpServletRequest request, Model model) {
		
		setCommonData(model);
		model.addAttribute(Constants.COMMAND, new TsTourGuide());

		int pn = ServletRequestUtils.getIntParameter(request, "pn", 1);
		Integer id = ServletRequestUtils.getIntParameter(request, "id", -1);
		boolean pre = ServletRequestUtils.getBooleanParameter(request, "pre",
				false);
		Page<TsTourGuide> page = null;
		if (id > 0) {
			if (pre) {
				page = tsTourGuideService.pre(id, pn);
			} else {
				page = tsTourGuideService.next(id, pn);
			}
		} else {
			page = tsTourGuideService.listAll(pn);
		}
		request.setAttribute("page", page);
		return "tstourguide/list";
	}

	// @RequestMapping(value = "/user/query", method = {RequestMethod.GET})
	// public String query(HttpServletRequest request, Model model,
	// @ModelAttribute("command") TsTourGuide command) {
	// setCommonData(model);
	// // model.addAttribute(Constants.COMMAND, command);
	// // int pn = ServletRequestUtils.getIntParameter(request, "pn", 1);
	// // model.addAttribute("page", userService.query(pn,
	// Constants.DEFAULT_PAGE_SIZE, command));
	//
	// return "user/list";
	// }
	//
	//
	// private void setCommonData(Model model) {
	// //设置通用属性
	// }
	//
	// @RequestMapping(value="/user/{userId}/view", method =
	// {RequestMethod.GET})
	// public String view(@PathVariable Integer topicId, HttpServletRequest
	// request) {
	// // request.setAttribute(Constants.COMMAND, userService.get(topicId));
	// return "user/view";
	// }
	//
	//
	//
	//
	@RequestMapping(value = "/tstourguide/add", method = { RequestMethod.GET })
	public String toAdd(Model model) {

		if (!model.containsAttribute(Constants.COMMAND)) {
			model.addAttribute(Constants.COMMAND, new TsTourGuide());
		}
		setCommonData(model);
		return "tstourguide/add";
	}

	@RequestMapping(value = "/tstourguide/{id}/edit", method = { RequestMethod.GET })
	public String toEdit(HttpServletRequest request, Model model,
			@PathVariable Integer id) {
		if (!model.containsAttribute(Constants.COMMAND)) {
			model.addAttribute(Constants.COMMAND, tsTourGuideService.get(id));
		}
		setCommonData(model);
		return "tstourguide/edit";
	}

	//
	// @RequestMapping(value = "/user/{id}/delete", method =
	// {RequestMethod.GET})
	// public String toDelete(@PathVariable Integer id) {
	// return "user/delete";
	// }
	//
	//
	// @RequestMapping(value = "/user/add", method = {RequestMethod.POST})
	// public String add(Model model, @ModelAttribute("command") @Valid
	// TsTourGuide command, BindingResult result) {
	//
	// //如果有验证错误 返回到form页面
	// if(result.hasErrors()) {
	// // model.addAttribute(Constants.COMMAND, command);
	// return toAdd(model);
	// }
	// // userService.save(command);
	// return "redirect:/user/success";
	// }
	//
	// @RequestMapping(value = "/user/{id}/update", method =
	// {RequestMethod.PUT})
	// public String update(Model model, @ModelAttribute("command") @Valid
	// TsTourGuide command, BindingResult result) {
	// // if(result.hasErrors()) {
	// // model.addAttribute(Constants.COMMAND, command);
	// // return toUpdate(model, command.getId());
	// // }
	// // userService.update(command);
	// return "redirect:/user/success";
	// }
	//
	// @RequestMapping(value = "/user/{id}/delete", method =
	// {RequestMethod.DELETE})
	// public String delete(@PathVariable Integer id) {
	// // userService.delete(id);
	// return "redirect:/user/success";
	// }
	//
	// @RequestMapping(value = "/user/success", method = {RequestMethod.GET})
	// public String success() {
	// return "user/success";
	// }

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		// binder.registerCustomEditor(Date.class, new DateEditor());
	}

}
